<%@ page import="com.ld.businesscard_manage.bean.BusinessCard" %>
<%@ page import="com.ld.businesscard_manage.service.BusinessCardService" %>
<%@ page import="com.ld.businesscard_manage.service.impl.BusinessCardServiceImpl" %>
<%@ page import="org.apache.commons.codec.digest.DigestUtils" %>
<%@ page import="com.ld.businesscard_manage.bean.User" %>
<%@ page import="com.ld.businesscard_manage.service.UserService" %>
<%@ page import="com.ld.businesscard_manage.service.impl.UserServiceImpl" %>
<%@ page import="java.sql.SQLOutput" %><%--
  Created by IntelliJ IDEA.
  User: 刘邓
  Date: 2023-06-10
  Time: 10:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改名片结果</title>
</head>
<body>
<%!
    private BusinessCardService cardService = new BusinessCardServiceImpl();
	private UserService userService = new UserServiceImpl();
%>
<%
    request.setCharacterEncoding("utf-8");
    BusinessCard card0 = (BusinessCard) (session.getAttribute("currentCard"));
    Integer id = card0.getId();
    String name0 = card0.getName();
	String name = new String(name0);
    Integer sex = card0.getSex();
	String account0 = card0.getAccount();
	String account = new String(account0);
    String password = card0.getPassword();
	String email = card0.getEmail();
    Integer valid = card0.getValid();
	if(!request.getParameter("password").equals(request.getParameter("repassword"))) {
		response.sendRedirect("/home/manageCard");
    }
	if (!request.getParameter("name").equals("")) {
        name = request.getParameter("name");
    }
	if(!request.getParameter("sex").equals("")){
        sex = Integer.parseInt(request.getParameter("sex"));
    }
	if(!request.getParameter("account").equals("")){
        account = request.getParameter("account");
    }
	if(!request.getParameter("password").equals("")){
        password = DigestUtils.md5Hex(request.getParameter("password"));
    }
	if(!request.getParameter("email").equals("")){
        email = request.getParameter("email");
    }
    User userByName = userService.getUserByName(account);
    if(userByName != null && !userByName.getUsername().equals(account0)){
        out.println("<h1>该账号已经存在<h1>");
    }
	else {
        System.out.println(account0);
		User user0 =  userService.getUserByName(account0);
        System.out.println(user0);
        User user = new User(user0.getId(), account, password, user0.getStatus());
        int i = userService.changeUserById(user);
		BusinessCard card = new BusinessCard(id,name,sex,account,password,email,card0.getValid());
        int j = cardService.updateCard(card);
        if (i == 0 || j == 0 ) {
            out.println("<h1>修改失败,请稍后再试!</h1>");
        } else {
            out.println("<h1>修改成功</h1>");
        }
    }
    out.println("<a href='/home/manageCard.jsp'>返回名片管理页</a>");
%>
</body>
</html>
